package com.hb.trans.utils

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import androidx.core.content.ContextCompat
import android.util.AttributeSet
import android.view.View
import com.hb.trans.R

class NumberProgress(val mContext: Context, val attrs: AttributeSet?, val defStyleAttr: Int) :
    View(mContext, attrs, defStyleAttr) {

    private var progressHeight = 20
    private var progressColor = R.color.blue_23
    private var otherColor = R.color.gray_f0
    private var currentProgress = 0
    private var maxProgress = 100.0f

    constructor(mContext: Context, attr: AttributeSet?) : this(mContext, attr, 0)
    constructor(mContext: Context) : this(mContext, null)

    override fun onDraw(canvas: Canvas?) {
        val width = this.width
        val height = this.height
        val radius = height * 0.5f

        val progressPaint = Paint().apply {
            color = ContextCompat.getColor(mContext, progressColor)
            style = Paint.Style.FILL
            isAntiAlias = true
            strokeCap = Paint.Cap.ROUND
            strokeWidth = height.toFloat()
        }
        val unreachProgressPaint = Paint().apply {
            color = ContextCompat.getColor(mContext, otherColor)
            style = Paint.Style.FILL
            isAntiAlias = true
            strokeCap = Paint.Cap.ROUND
            strokeWidth = height.toFloat()
        }
        canvas?.drawLine(
            width * currentProgress / maxProgress + radius,
            radius,
            width - radius,
            radius,
            unreachProgressPaint
        )

        if (currentProgress != 0) {
            canvas?.drawLine(
                radius,
                radius,
                width * currentProgress / maxProgress,
                radius,
                progressPaint
            )
        }
    }

    fun setProgress(progress: Int) {
        currentProgress = progress
        invalidate()
    }

    fun setPrecent(precent: Float) {
        currentProgress = (100 * precent).toInt()
        invalidate()
    }
}